﻿using System;

namespace QianJiaInfo.Models
{
    /// <summary>
    /// 居民信息
    /// </summary>
    public class Resident : CommonModel
    {
        /*
         * 1.ID
         * 2.姓名
         * 3.性别，从身份证号码上带出来，只读 不存 数据库
         * 4.身份证号码
         * 5.所属户口
         * 6.状态-迁出，删除，死亡
         * 7.出生日期-从身份证号码上带出， 只读 不存 数据库
         * 8.与户主关系 ，枚举
         * 9.手机号
         * 10.务工地
         * 11.备注
         *  
         */
        /// <summary>
        /// 
        /// </summary>


        public int ResidentId { get; set; }
        /// <summary>
        /// 姓名
        /// </summary>


        public string Name { get; set; }
        /// <summary>
        /// 性别
        /// </summary>

        public string Gender
        {
            get
            {
                if (string.IsNullOrWhiteSpace(CardId))
                {
                    return "未提供身份证号码";
                }
                else
                {
                    var sex = CardId.Substring(16, 1);
                    int sexInt = int.Parse(sex);
                    return sexInt % 2 == 0 ? "女" : "男";
                }

            }
        }

        public int Age
        {
            get
            {


                if (string.IsNullOrWhiteSpace(CardId))
                {
                    throw new Exception("身份证号码未提供");
                }
                else
                {
                    var birth = CardId.Substring(6, 8);
                    DateTime birthdate = DateTime.Parse(birth);
                    DateTime now = DateTime.Now;
                    int age = now.Year - birthdate.Year;
                    if (now.Month < birthdate.Month || (now.Month == birthdate.Month && now.Day < birthdate.Day))
                    {
                        age--;
                    }
                    return age < 0 ? 0 : age;

                }

            }
        }
        /// <summary>
        /// 身份证号码
        /// </summary>

        public string CardId { get; set; }
        /// <summary>
        ///  户
        /// </summary>
        public Residence ResidenceBlong { get; set; }
        /// <summary>
        /// 状态
        /// </summary>

        public PersonStatu State { get; set; }
        /// <summary>
        /// 出生日期
        /// </summary>
        public DateTime BirthDate
        {
            get
            {
                if (string.IsNullOrWhiteSpace(CardId))
                {
                    throw new Exception("身份证号码未提供");
                }
                else
                {
                    var birth = CardId.Substring(6, 8);
                    return DateTime.Parse(birth);
                }


            }
        }
        /// <summary>
        /// 和户主的关系
        /// </summary>
        public PersonRelationship Relationship { get; set; }
        /// <summary>
        /// 电话号码
        /// </summary>
        public string PhoneNumber { get; set; }
        /// <summary>
        /// 工作地
        /// </summary>
        public string WorkAddress { get; set; }
        /// <summary>
        /// 备注
        /// </summary>
        public string Remark { get; set; }
        /// <summary>
        /// 是不是户主
        /// </summary>

        public bool HoseHold { get; set; }
    }
}
